package com.zekai.calculator;

import android.app.Activity;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.activity.result.ActivityResultLauncher;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class HistoryAdapter extends RecyclerView.Adapter<HistoryAdapter.ViewHolder> {

    private List<String> expressions;
    private List<String> results;
    private ActivityResultLauncher<Intent> helpLauncher;

    // 构造函数
    public HistoryAdapter(List<String> expressions, List<String> results, ActivityResultLauncher<Intent> helpLauncher) {
        this.expressions = expressions;
        this.results = results;
        this.helpLauncher = helpLauncher;
    }

    // 创建 ViewHolder
    public static class ViewHolder extends RecyclerView.ViewHolder {
        TextView expressionTextView;
        TextView resultTextView;

        public ViewHolder(View itemView) {
            super(itemView);
            expressionTextView = itemView.findViewById(R.id.history_expression);
            resultTextView = itemView.findViewById(R.id.history_result);
        }
    }

    // 创建 ViewHolder
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.history_item, parent, false);
        return new ViewHolder(view);
    }

    // 绑定数据到 ViewHolder
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.expressionTextView.setText(expressions.get(position));
        holder.resultTextView.setText(results.get(position));
        // 设置点击事件
        // 在这里为每个历史记录项添加点击监听器
        holder.itemView.setOnClickListener(v -> {
            String selectedExpression = expressions.get(position);
            Intent intent = new Intent(v.getContext(), MainActivity.class);
            intent.putExtra("expression", selectedExpression);
            helpLauncher.launch(intent);
        });

    }

    // 获取数据项的数量
    @Override
    public int getItemCount() {
        return expressions.size();
    }


}
